using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using MacomberMap.Common.Internals;
using MacomberMap.Common.User_Interfaces.Components;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.Convertors
{
    /// <summary>
    /// This class allows for conversions between strings and MM_KVLevels.
    /// </summary>
    public class MM_KVLevel_Locator : TypeConverter
    {
       /// <summary>
       /// Indicate standard values are supported
       /// </summary>
       /// <param name="context"></param>
       /// <returns></returns>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Inidicate standard values are supported exclusively
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        /// <summary>
        /// Return a standard value collection of KV levels by string
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new System.ComponentModel.TypeConverter.StandardValuesCollection(MM_Overall_Display.KVLevels.Keys);
        }

        /// <summary>
        /// Convert from a string to KV level
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
                if (MM_Overall_Display.KVLevels.ContainsKey((string)value))
                    return MM_Overall_Display.KVLevels[(string)value];
                else
                    return null;
            else
                return base.ConvertFrom(context, culture, value);
        }
      
        /// <summary>
        /// Indicate a string can be converted to a KV level
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sourceType"></param>
        /// <returns></returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String))
                return true;
            else            
                return base.CanConvertFrom(context, sourceType);
        }

        /// <summary>
        /// Indicate a KV level or series of KV levels can be converted to a string.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            /*if (destinationType == typeof(MM_KVLevel) || destinationType == typeof(MM_KVLevel[]))
                return true;
            else*/
                return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Convert a KV level or series of KV levels to a string.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
                return "";
            else if (value is MM_KVLevel)
                return ((MM_KVLevel)value).ToString();
            else if (value is IEnumerable<MM_KVLevel>)
            {
                StringBuilder sB = new StringBuilder();
                foreach (MM_KVLevel KVLevel in (IEnumerable<MM_KVLevel>)value)
                    sB.Append((sB.Length > 0 ? ", " : "") + KVLevel.ToString());
                return sB.ToString();
            }
            else
                return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
